package com.example.administrator.chenencryption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
private  String path="http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=%s";
    private String path1;
    //%E6%AF%9B%E6%B3%BD%E4%B8%9C 毛泽东
    private String path2="https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=%E6%AF%9B%E6%B3%BD%E4%B8%9C";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initdata();
        try {
          String encode=  URLEncoder.encode("毛泽东","UTF-8");
            Log.d("flag", "-----------onCreate: "+encode);
            path1 = path+encode;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private void initdata() {


        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is=null;
                ByteArrayOutputStream baos=null;
                try {

                    URL url=new URL(path2);
                    HttpURLConnection huc= (HttpURLConnection) url.openConnection();
                       huc.setConnectTimeout(5000);
                    if (huc.getResponseCode()==200){
                      is=   huc.getInputStream();
                        baos=new ByteArrayOutputStream();
                        int len=0;
                        byte[] buf=new byte[1024*8];
                        while ((len=is.read(buf))!=-1){
                            baos.write(buf,0,len);
                        }
                        byte[] bytes = baos.toByteArray();
                        String s = new String(bytes);
                        Log.d("flag", "----------run: "+s);
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (baos != null) {
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }if (is!=null){
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }


            }
        }).start();
    }
}
